package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController("adminDishController")
@Slf4j
@RequestMapping("/admin/dish")
@Api(tags = "后台菜品管理")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;


    //清理redis缓存方法
    public void cleanRedisCache(String pattern) {
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }

    @ApiOperation("新增菜品")
    @PostMapping
    public Result add(@RequestBody DishDTO dishDTO) {
        log.info("新增菜品：{}", dishDTO);
        dishService.addWithFlavor(dishDTO);

        String key = "dish_"+dishDTO.getCategoryId();
        cleanRedisCache(key);

        return Result.success();
    }

    @ApiOperation("分页展示菜品")
    @GetMapping("/page")
    public Result pageQuery(DishPageQueryDTO dishPageQueryDTO){
        log.info("分页查询菜品：{}", dishPageQueryDTO);
        PageResult pageResult = dishService.pageQuery(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    @ApiOperation("批量删除菜品")
    @DeleteMapping
    public Result delete(@RequestParam List<Long> ids){
        log.info("删除菜品：{}", ids);
        dishService.delete(ids);
        cleanRedisCache("dish_*");
        return Result.success();
    }

    @ApiOperation("根据id查询菜品")
    @GetMapping("/{id}")
    public Result getById(@PathVariable Long id){
        log.info("查询菜品：{}", id);
        DishVO dishVO = dishService.getById(id);
        return Result.success(dishVO);
    }

    @ApiOperation("修改菜品")
    @PutMapping
    public Result update(@RequestBody DishDTO dishDTO){
        log.info("修改菜品：{}", dishDTO);
        dishService.update(dishDTO);
        cleanRedisCache("dish_*");
        return Result.success();
    }

    @ApiOperation("根据分类id查询菜品")
    @GetMapping("/list")
    public Result selectByCategoryId(Dish dish){
        log.info("查询分类id为{}的菜品", dish);
        List<Dish> dishList = dishService.selectByCategoryId(dish.getCategoryId());
        return Result.success(dishList);
    }

    @ApiOperation("修改起售和停售状态")
    @PostMapping("/status/{status}")
    public Result updateStatus(@PathVariable String status, String id){
        log.info("修改菜品{}的状态为{}", id, status);
        dishService.updateStatus(status, id);
        cleanRedisCache("dish_*");
        return Result.success();
    }

    @ApiOperation("根据id删除菜品")
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Long id){
        log.info("删除菜品：{}", id);
        dishService.deleteById(id);
        cleanRedisCache("dish_*");
        return Result.success();
    }

}
